package com.demo.service.impl;


import java.util.List;

import com.demo.entity.enums.DeviceStatusEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.DeviceInfo;
import com.demo.entity.po.DeviceInventory;

import com.demo.entity.query.DeviceInfoQuery;
import com.demo.entity.query.DeviceInventoryQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.DeviceInfoMapper;
import com.demo.service.DeviceInventoryService;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.DeviceInventoryMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:serviceImpl
 * @Date:2025-03-11
 * @author：author
*/
@Service("deviceInventoryService")
public class DeviceInventoryServiceImpl implements DeviceInventoryService{

	@Resource
	private DeviceInventoryMapper<DeviceInventory,DeviceInventoryQuery> deviceInventoryMapper;

	@Resource
	private DeviceInfoMapper<DeviceInfo, DeviceInfoQuery> deviceInfoMapper;
    @Qualifier("requestMappingHandlerMapping")
    @Autowired
    private BeanNameAware beanNameAware;


	/**
	 * 根据条件查询列表
	 */
	public List<DeviceInventory> findListByParam(DeviceInventoryQuery query) { 
		return this.deviceInventoryMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(DeviceInventoryQuery query){
		return this.deviceInventoryMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<DeviceInventory> findListByPage(DeviceInventoryQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<DeviceInventory> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(DeviceInventory bean) { 
		return this.deviceInventoryMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<DeviceInventory> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceInventoryMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<DeviceInventory> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceInventoryMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据InventoryId查询
	 */
	public DeviceInventory getDeviceInventoryByInventoryId(Integer inventoryId) { 
		return this.deviceInventoryMapper.selectByInventoryId(inventoryId);
	}

	/**
	 * 根据InventoryId更新
	 */
	public Integer updateDeviceInventoryByInventoryId(DeviceInventory bean, Integer inventoryId) { 
		return this.deviceInventoryMapper.updateByInventoryId(bean,inventoryId);
	}

	/**
	 * 根据InventoryId删除
	 */
	public Integer deleteDeviceInventoryByInventoryId(Integer inventoryId) { 
		return this.deviceInventoryMapper.deleteByInventoryId(inventoryId);
	}

	/**
	 * 保存库存明细信息
	 * @param bean
	 */
	@Override
	public void saveInventory(DeviceInventory bean) {
		// 校验设备状态参数
		DeviceStatusEnum statusEnum = DeviceStatusEnum.getByStatus(bean.getStatus());
		if (statusEnum == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		DeviceInfo deviceInfoDB = deviceInfoMapper.selectByDeviceId(bean.getEquipmentId());
		if (deviceInfoDB == null) {
			throw new BusinessException("实验器材不存在");
		}
		// 拦截直接调用接口
		if (bean.getAvailableQuantity()>bean.getTotalQuantity()){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if (bean.getInventoryId() == null) {
			deviceInventoryMapper.insert(bean);
		}else {
			deviceInventoryMapper.updateByInventoryId(bean,bean.getInventoryId());
		}
	}
	/**
	 * 删除库存明细
	 * @param inventoryIds
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delInventory(Integer[] inventoryIds) {
		if (inventoryIds == null || inventoryIds.length == 0) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		for (Integer inventoryId : inventoryIds) {
			deviceInventoryMapper.deleteByInventoryId(inventoryId);
		}
	}
}